/*
 * COMP6471 Project
 *  
 * This file is created by Yuan Tao (ewan.msn@gmail.com)
 * Licensed under GNU GPL v3
 * 
 * $Author: ewan.msn@gmail.com $
 * $Date: 2011-11-05 19:52:51 +0000 (Sat, 05 Nov 2011) $
 * $Rev: 49 $
 * $HeadURL: http://comp6471.googlecode.com/svn/Project4/src/util/logger.java $
 * 
 */

package util;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import kklib.Layout;


class LoggerTextFormat extends Formatter { 
    @Override 
    public String format(LogRecord record) { 
    	return record.getLevel() + ": " + record.getMessage() + "\n"; 
    } 
}

class OutputTextFormat extends Formatter { 
    @Override 
    public String format(LogRecord record) { 
    	return record.getMessage() + "\n"; 
    } 
}

public class logger {
	public static Logger log = Logger.getLogger("Project1");
	public static Logger result = Logger.getLogger("Project1Result");
		
	public static void init() {
//		ConsoleHandler consoleHandle = new ConsoleHandler();
//		consoleHandle.setLevel(Level.ALL);
//		log.addHandler(consoleHandle);
		log.setUseParentHandlers(false);
		//result.setUseParentHandlers(false);
		
		// add a file handler
		String path = System.getProperty("user.dir") + "\\log\\log.txt";
		FileHandler fileHandle;
		try {
			fileHandle = new FileHandler(path);
			fileHandle.setLevel(Level.SEVERE);
			fileHandle.setFormatter(new LoggerTextFormat());
			log.addHandler(fileHandle);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// create a logger to show the result of the program.
		path = System.getProperty("user.dir") + "\\output\\result.txt";
		try {
			fileHandle = new FileHandler(path);
			fileHandle.setLevel(Level.ALL);
			fileHandle.setFormatter(new OutputTextFormat());
			result.addHandler(fileHandle);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void info(String msg) {
		log.info(msg);
	}
	
	public static void err(String msg) {
		log.severe(msg);
	}
	
	public static void output(String msg) {
		result.info(msg);
		log.info(msg);
	}
	
	public static void parserErr(String file, String msg) {
		String strMsg = "File: " + file + " " + "Syntax Error: " + msg; 
		result.info(strMsg);
		log.severe(strMsg);
	}

	/*
	 * +---+---+    						(first line)
       |       |    show cage formulas		(middle line)
       +   +---+    						(bottom line)
       |   |   |    show cage formulas
       +---+---+    						(last line)
	 */
	public static void printOutGame(Layout layout) {
		StringBuffer sb = new StringBuffer();
		
		//sb.append("Game Name: " + layout.gameName + "\n");
		
		// first line
		sb.append("+");
		for (int i = 0; i < layout.gameSize; i++) {
			sb.append("---+");
		}
		sb.append("\n");
		
		for (int i = 0; i < layout.gameSize; i++) {
			int val = 0, data = 0, nextData = 0;
			
			// middle line
			sb.append("|");
			for (int j = 0; j < layout.gameSize - 1; j++) {
				val = layout.rows.get(i * layout.gameSize + j).value;
				data = layout.rows.get(i * layout.gameSize + j).data;
				nextData = layout.rows.get(i * layout.gameSize + j + 1).data;
				
				sb.append(" " + (val != 0 ? val : " ") + (data == nextData ? "  " : " |"));
			}
			val = layout.rows.get(i * layout.gameSize + layout.gameSize - 1).value;
			sb.append(" " + (val != 0 ? val : " ") + " |");
			
			// show cage formulas
			sb.append("\t");
			for (int j = 0; j <= layout.gameSize / 2; j++) {
				int index = i * (layout.gameSize / 2 + 1) + j;
				if (index < layout.txtFileFormulas.size()) {
					sb.append("[" + layout.txtFileFormulas.get(index) + "] ");
				}
			}
			sb.append("\n");
			
			// bottom line
			if (i != layout.gameSize - 1) {
				sb.append("+");
				for (int j = 0; j < layout.gameSize; j++) {
					data = layout.rows.get(i * layout.gameSize + j).data;
					nextData = layout.rows.get((i + 1) * layout.gameSize + j).data;
					
					sb.append(data == nextData ? "   +" : "---+");
				}
				sb.append("\n");
			}
		}
		
		// last line
		sb.append("+");
		for (int k = 0; k < layout.gameSize; k++) {
			sb.append("---+");
		}
		sb.append("\n");
		
		output(sb.toString());
	}
}
